/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JPnlBackgroundTable.java
 *
 * Created on 29/05/2011, 13:12:39
 */
package systole.view.tabs.results.background;

import java.util.Iterator;
import java.util.List;

/**
 *
 * @author jmj
 */
public class JPnlBackgroundTable extends javax.swing.JPanel {

    private BackgroundTableModel tableModel;
    private List data;
    private String entityTitle;

    /** Creates new form JPnlBackgroundTable
     * @param data
     * @param entityTitle 
     */
    public JPnlBackgroundTable(List data, String entityTitle) {
        initComponents();
        this.data = data;
        this.entityTitle = entityTitle;
        this.initForm();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPnlCenter = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jXTableBackground = new org.jdesktop.swingx.JXTable();
        jPnlCenterTop = new javax.swing.JPanel();
        jPnlCenterBottom = new javax.swing.JPanel();
        jPnlCenterRigth = new javax.swing.JPanel();
        jPnlCenterLeft = new javax.swing.JPanel();
        jPnlBottom = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTxtExample = new javax.swing.JTextArea();
        jPnlBottomTop = new javax.swing.JPanel();
        jPnlBottomBottom = new javax.swing.JPanel();
        jPnlBottomRigth = new javax.swing.JPanel();
        jPnlBottomLeft = new javax.swing.JPanel();

        setName("Form"); // NOI18N
        setPreferredSize(new java.awt.Dimension(350, 300));
        setLayout(new java.awt.BorderLayout());

        jPnlCenter.setName("jPnlCenter"); // NOI18N
        jPnlCenter.setLayout(new java.awt.BorderLayout());

        jScrollPane2.setName("jScrollPane2"); // NOI18N
        jScrollPane2.setPreferredSize(new java.awt.Dimension(340, 180));

        jXTableBackground.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jXTableBackground.setName("jXTableBackground"); // NOI18N
        jScrollPane2.setViewportView(jXTableBackground);

        jPnlCenter.add(jScrollPane2, java.awt.BorderLayout.CENTER);

        jPnlCenterTop.setName("jPnlCenterTop"); // NOI18N
        jPnlCenter.add(jPnlCenterTop, java.awt.BorderLayout.PAGE_START);

        jPnlCenterBottom.setName("jPnlCenterBottom"); // NOI18N
        jPnlCenter.add(jPnlCenterBottom, java.awt.BorderLayout.PAGE_END);

        jPnlCenterRigth.setName("jPnlCenterRigth"); // NOI18N
        jPnlCenter.add(jPnlCenterRigth, java.awt.BorderLayout.LINE_END);

        jPnlCenterLeft.setName("jPnlCenterLeft"); // NOI18N
        jPnlCenter.add(jPnlCenterLeft, java.awt.BorderLayout.LINE_START);

        add(jPnlCenter, java.awt.BorderLayout.CENTER);

        jPnlBottom.setName("jPnlBottom"); // NOI18N
        jPnlBottom.setLayout(new java.awt.BorderLayout());

        jScrollPane1.setName("jScrollPane1"); // NOI18N
        jScrollPane1.setPreferredSize(new java.awt.Dimension(340, 80));

        jTxtExample.setColumns(20);
        jTxtExample.setLineWrap(true);
        jTxtExample.setRows(4);
        jTxtExample.setWrapStyleWord(true);
        jTxtExample.setName("jTxtExample"); // NOI18N
        jScrollPane1.setViewportView(jTxtExample);

        jPnlBottom.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jPnlBottomTop.setName("jPnlBottomTop"); // NOI18N
        jPnlBottom.add(jPnlBottomTop, java.awt.BorderLayout.PAGE_START);

        jPnlBottomBottom.setName("jPnlBottomBottom"); // NOI18N
        jPnlBottom.add(jPnlBottomBottom, java.awt.BorderLayout.PAGE_END);

        jPnlBottomRigth.setName("jPnlBottomRigth"); // NOI18N
        jPnlBottom.add(jPnlBottomRigth, java.awt.BorderLayout.LINE_END);

        jPnlBottomLeft.setName("jPnlBottomLeft"); // NOI18N
        jPnlBottom.add(jPnlBottomLeft, java.awt.BorderLayout.LINE_START);

        add(jPnlBottom, java.awt.BorderLayout.SOUTH);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPnlBottom;
    private javax.swing.JPanel jPnlBottomBottom;
    private javax.swing.JPanel jPnlBottomLeft;
    private javax.swing.JPanel jPnlBottomRigth;
    private javax.swing.JPanel jPnlBottomTop;
    private javax.swing.JPanel jPnlCenter;
    private javax.swing.JPanel jPnlCenterBottom;
    private javax.swing.JPanel jPnlCenterLeft;
    private javax.swing.JPanel jPnlCenterRigth;
    private javax.swing.JPanel jPnlCenterTop;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTxtExample;
    private org.jdesktop.swingx.JXTable jXTableBackground;
    // End of variables declaration//GEN-END:variables

    public void initForm() {
        this.tableModel = new BackgroundTableModel(this.data);
        this.jXTableBackground.setModel(tableModel);
        this.jXTableBackground.getModel().addTableModelListener(new BackgroundTableModelListener(this));
        this.jXTableBackground.packAll();
    }

    public void updateExample() {
        this.jTxtExample.removeAll();
        String example = "";
        int i = 0;
        while (this.data.size() > i) {
            if ((Boolean) this.tableModel.getValueAt(i, BackgroundTableModel.INDEX_SELECT)) {
                example = (!example.isEmpty() ? ", " : "") + (this.data.get(i).toString());
            }
            i++;
        }
        if (!example.isEmpty()) {
            example = entityTitle + ": " + example;
        }
        this.jTxtExample.setText(example);
    }

    /**
     *
     * @return if list is empty
     */
    public boolean isEmptyList() {
        return this.data.isEmpty();
    }

    /**
     * 
     * @return background text
     */
    public String getPatientBackground() {
        return this.jTxtExample.getText();
    }

    public void loadData(String comments) {
        if ((comments != null) && (!comments.isEmpty()) && (!this.data.isEmpty())) {
            Iterator it = this.data.iterator();
            int i = 0;
            while (it.hasNext()) {
                if (comments.contains(it.next().toString())) {
                    this.tableModel.setValueAt(true, i, 0);
                }
                i++;
            }
        }
    }
}
